Passed
Push — master ( 8ff0a1...a2cde2 )
by Rafael S.
01:21
created

api.js ➔ unpackArray   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
/*!
2
 * byte-data
3
 * Readable data to and from byte buffers.
4
 * Copyright (c) 2017 Rafael da Silva Rocha.
5
 * https://github.com/rochars/byte-data
6
 *
7
 */
8
9
let toBytes = require("../src/to-bytes");
10
let fromBytes = require("../src/from-bytes");
11
let helpers = require("../src/helpers");
12
13
/**
14
 * Turn a number or string into a byte buffer.
15
 * @param {number|string} value The value.
16
 * @param {Object} type One of the available types.
17
 * @param {number} base The base of the output. Optional. Default is 10.
18
 * @return {!Array<number>|!Array<string>}
19
 */
20
function pack(value, type, base=10) {
21
    let theType = helpers.getType(type, base, true);
22
    value = theType.char ? value.slice(0, type.bits / 8) : value;
23
    return toBytes.toBytes(helpers.turnToArray(value), theType);
24
}
25
26
/**
27
 * Turn a byte buffer into a readable value.
28
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes.
29
 * @param {Object} type One of the available types.
30
 * @param {number} base The base of the input. Optional. Default is 10.
31
 * @return {number|string}
32
 */
33
function unpack(buffer, type, base=10) {
34
    return fromBytes.fromBytes(buffer, helpers.getType(type, base, true));
35
}
36
37
/**
38
 * Turn a array of numbers into a byte buffer.
39
 * @param {!Array<number>|string} values The values.
40
 * @param {Object} type One of the available types.
41
 * @param {number} base The base of the output. Optional. Default is 10.
42
 * @return {!Array<number>|!Array<string>}
43
 */
44
function packArray(values, type, base=10) {
45
    return toBytes.toBytes(values, helpers.getType(type, base, false));
46
}
47
48
/**
49
 * Turn a byte array into a sequence of readable values.
50
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array.
51
 * @param {Object} type One of the available types.
52
 * @param {number} base The base of the input. Optional. Default is 10.
53
 * @return {!Array<number>|string}
54
 */
55
function unpackArray(buffer, type, base=10) {
56
    return fromBytes.fromBytes(buffer, helpers.getType(type, base, false));
57
}
58
59
/**
60
 * Find and return the start index of some string.
61
 * Return -1 if the string is not found.
62
 * @param {!Array<number>|Uint8Array} buffer A byte buffer.
63
 * @param {string} text Some string to look for.
64
 * @return {number} The start index of the first occurrence, -1 if not found
65
 */
66
function findString(buffer, text) {
67
    let found = "";
68
    for (let i = 0; i < buffer.length; i++) {
69
        found = unpack(
70
            buffer.slice(i, i + text.length + 1),
71
            {"bits": text.length * 8, "char": true});
72
        if (found == text) {
73
            return i;
74
        }
75
    }
76
    return -1;
77
}
78
79
/**
80
 * Turn a struct into a byte buffer.
81
 * A struct is an array of values of not necessarily the same type.
82
 * @param {Array} struct The struct values.
83
 * @param {!Array<Object>} def The struct type definition.
84
 * @param {number} base The base of the output. Optional. Default is 10.
85
 * @return {!Array<number>|!Array<string>}
86
 */
87
function packStruct(struct, def, base=10) {
88
    let bytes = [];
89
    for (let i = 0; i < struct.length; i++) {
90
        bytes = bytes.concat(pack(struct[i], def[i], base));
91
    }
92
    return bytes;
93
}
94
95
/**
96
 * Turn a byte buffer into a structure.
97
 * A struct is an array of values of not necessarily the same type.
98
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte buffer.
99
 * @param {!Array<Object>} def The struct type definition.
100
 * @param {number} base The base of the input. Optional. Default is 10.
101
 * @return {Array}
102
 */
103
function unpackStruct(buffer, def, base=10) {
104
    let struct = [];
105
    let i = 0;
106
    let j = 0;
107
    while (j < buffer.length) {
108
        let bits = def[i].bits < 8 ? 1 : def[i].bits / 8;
109
        struct = struct.concat(
110
                unpack(buffer.slice(j, j + bits), def[i], base)
111
            );
112
        j += bits;
113
        i++;
114
    }
115
    return struct;
116
}
117
118
module.exports.pack = pack;
119
module.exports.findString = findString;
120
module.exports.unpack = unpack;
121
module.exports.packArray = packArray;
122
module.exports.unpackArray = unpackArray;
123
module.exports.unpackStruct = unpackStruct;
124
module.exports.packStruct = packStruct;
125